﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;

namespace CodeTalk.CTSpeechCommands
{
    class LearnVariable : SpeechCommand
    {
        internal override GrammarBuilder GrammarBld(Context context, Context[] libraries)
        {
            return new GrammarBuilder("learn variable");
        }

        public override string Name
        {
            get { return "Learn variable"; }
        }

        public override string Description
        {
            get { return "Reads the currently selected text, builds variable from it and commits this variable into the current Context. The Category of the variable will be determined from the notation of the variable. "; }
        }

        internal override bool CanSpokenFormMatch(string spokenForm)
        {
            return spokenForm == "learn variable";
        }

        internal override object Execute(SpeechMode callingMode, string recognizedText, SemanticValue semValue)
        {
            string selectedText = InputHandler.ReadSelectedText();
            if (selectedText != null)
            {
                selectedText = selectedText.Trim();
                SymbolCategory? category = CodeSymbol.DetermineCategory(selectedText);
                if (category.HasValue)
                {
                    string pronounceable = TextUtilz.MakePronounceable(selectedText).ToLowerInvariant();
                    string errMsg = "";
                    uint id = callingMode.CurrentContext.CreateNewSymbol(selectedText, pronounceable, category.Value, callingMode, ref errMsg);
                    if (id == 0)
                        callingMode.CurrentGuiHandler.SetErrorFeedback("Could not create new variable: " + errMsg);
                    else
                    {
                        callingMode.CurrentGuiHandler.SetPrimaryFeedback("Variable learned: " + selectedText + " (" + pronounceable + ")");
                        callingMode.RecompileGrammar();
                        callingMode.CurrentGuiHandler.RefreshSymbolView();
                        return id;
                    }
                }
                else
                {
                    callingMode.CurrentGuiHandler.SetErrorFeedback("Selected text is not a valid variable!");
                }
            }
            else
            {
                callingMode.CurrentGuiHandler.SetErrorFeedback("No valid text selected to learn!");
            }
            return null;
        }

        internal override void Undo(SpeechMode callingMode, string recognizedText, SemanticValue semValue, object undoObject)
        {
            if (undoObject != null)
            {
                uint id = (uint)undoObject;
                callingMode.CurrentContext.DeleteSymbolWithID(id);
                callingMode.CurrentOutputHandler.SendKeys("^z");
                callingMode.CurrentGuiHandler.RefreshSymbolView();
            }
        }
    }
}
